﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Assignment4
{
    class ReadFile
    {
        public static List<Example> Read(string fileName)
        {
            List<Example> examples = new List<Example>();
            char[] delimiter = { ' ', '\t' };
            List<string> attributeTypes = new List<string>();
            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read)))
                {
                    string currentLine = reader.ReadLine();
                    while (currentLine == "" || currentLine.IndexOf("//") == 0)
                    {
                        currentLine = reader.ReadLine();
                    }

                    Example.ActionCollection.Add(currentLine);
                    
                    currentLine = reader.ReadLine();
                    while (currentLine == "" || currentLine.IndexOf("//") == 0)
                    {
                        currentLine = reader.ReadLine();
                    }

                    Example.ActionCollection.Add(currentLine);
                    
                    currentLine = reader.ReadLine();
                    while (currentLine == "" || currentLine.IndexOf("//") == 0)
                    {
                        currentLine = reader.ReadLine();
                    }
                    
                    int numberOfFeatures = int.Parse(currentLine);
                    for (int i = 0; i < numberOfFeatures; i++)
                    {
                        currentLine = reader.ReadLine();
                        while (currentLine == "" || currentLine.IndexOf("//") == 0)
                        {
                            currentLine = reader.ReadLine();
                        }

                        string[] atributes = currentLine.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                        List<string> attributeValues = new List<string>();
                        attributeValues.Add(atributes[1]);
                        attributeValues.Add(atributes[2]);
                        attributeTypes.Add(atributes[0]);
                        try
                        {
                            Example.AttributeCollection.Add(atributes[0], attributeValues);
                        }
                        catch (ArgumentException)
                        {

                        }
                    }
                    
                    currentLine = reader.ReadLine();
                    while (currentLine == "" || currentLine.IndexOf("//") == 0)
                    {
                        currentLine = reader.ReadLine();
                    }
                    int numberOfExamples = int.Parse(currentLine);
                    for (int i = 0; i < numberOfExamples; i++)
                    {
                        currentLine = reader.ReadLine();
                        while (currentLine == "" || currentLine.IndexOf("//") == 0)
                        {
                            currentLine = reader.ReadLine();
                        }
                        string[] tmp = currentLine.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                        Example example = new Example(tmp[0]);
                        example.Action = tmp[1];
                        for (int j = 2; j < tmp.Length; j++)
                        {
                            example.addAttribute(attributeTypes[j - 2], tmp[j]);
                        }
                        examples.Add(example);
                    }
                    int a = 0;
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Cannot find file {0}", fileName);
                Console.WriteLine();
                Environment.Exit(0);
            }
            return examples;
        }
    }
}
